#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  string str;
  cin >> str;
  str = ' ' + str;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i ++) cin >> v[i];
  vector<vector<int>> dp(n + 1, vector<int>(n + 1));
  for (int l = n; l >= 1; l --) {
    for (int r = l; r <= n; r ++) {
      if (str[l] == '(' && str[r] == ')') {
        dp[l][r] = (l + 1 == r) || dp[l + 1][r - 1];
      }
      for (int k = l; k < r; k ++) {
        dp[l][r] |= dp[l][k] && dp[k + 1][r];
      }
    }
  }
  debug(dp);
  vector<int> linear(n + 1);
  for (int i = 1; i <= n; i ++) {
    linear[i] = linear[i - 1];
    for (int j = 1; j <= i; j ++) {
      if (dp[j][i]) {
        linear[i] = max(linear[i], linear[j - 1] + v[i] - v[j]);
      }
    }
  }
  debug(linear);
  cout << linear[n] << endl;
}